package com.pug.controller;

import com.github.pagehelper.PageInfo;
import com.pug.entity.User;
import com.pug.exception.BusinessException;
import com.pug.results.Code;
import com.pug.results.Result;
import com.pug.service.user.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @PostMapping
    public Result save(User user) {

        boolean flag = userService.save(user);
        return new Result(flag ? Code.SAVE_OK : Code.SAVE_ERROR);
    }

    @PutMapping
    public Result update(User user) {
        boolean flag = userService.update(user);
        return new Result(flag ? Code.UPDATE_OK : Code.UPDATE_ERROR);
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        boolean flag = userService.delete(id);
        return new Result(flag ? Code.DELETE_OK : Code.DELETE_ERROR);
    }

    @GetMapping("/{id}")
    public Result get(@PathVariable("id") Integer id) {
        User user = userService.get(id);
        //模拟出现异常，使用条件控制，便于测试结果
        if (id == 10) throw new BusinessException("查询出错啦，请重试！", Code.GET_ERROR);
        return new Result(null != user ? Code.GET_OK : Code.GET_ERROR, user);
    }

    @GetMapping("/{page}/{size}")
    public Result getAll(@PathVariable Integer page, @PathVariable Integer size) {
        PageInfo<User> all = userService.getAll(page, size);
        return new Result(null != all ? Code.GET_OK : Code.GET_ERROR, all);
    }

    @PostMapping("/login")
    public Result login(String userName, String password) {
        User user = userService.login(userName, password);
        return new Result(null != user ? Code.GET_OK : Code.GET_ERROR, user);
    }

}
